﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Reflection;
using System.Web.Routing;
using System.Web.Configuration;

namespace Sharp
{
   
    public class Error : Handler
    {
        public Error() { }


        [HttpHandler("error")]
        public void  ErrorPage()
        {
            RenderError(context.ApplicationInstance);
        }

        public void RenderError(HttpApplication app)
        {  
              
            if (app.Context.AllErrors != null)
                foreach (var err in app.Context.AllErrors)
                {
                    if (context.IsDebuggingEnabled)
                        sb.ErrorMsg = ("error" + ": " + err + "\n");
                    else
                       sb.ErrorMsg = ("error" + ": " + err.Message + "\n");
                }
            

            if (contentType == HttpHandler.ContentType.JSON)
            {
                
            }
            else
            {                 
                RenderTestForm(""); 
            }

        }

        public Error(object sender, EventArgs e)
        {
            HttpApplication app = ((HttpApplication)sender);
            //if (app.Context.AllErrors[0].Message == "No http handler was found for request type 'GET'")
            //{
            //    //Some things used to require a project name, this adds it to the web.config.
            //    if (ConfigurationManager.AppSettings["ProjectName"] == null)
            //    {
            //        var config = WebConfigurationManager.OpenWebConfiguration("~");
            //        var section = (AppSettingsSection)config.GetSection("appSettings");
            //        section.Settings.Add("ProjectName", "Sharp");
            //        config.Save();
            //    }
            //    app.Context.Response.Redirect("/sitemap");
            //}
            //else
            {

                RenderError(app);


            }
            //app.Context.Response.End();
            ProcessRequest(context);
        }

    }
}